#!/bin/sh
#(c) Copyright 2010 Barry Kauler bkhome.org
#Lesser GPL licence v2 (/usr/share/doc/legal/lgpl-2.1.txt)
#called from 'connectwizard'
#100227 BK initial creation of script.
#100306 added Barry's Simple Network Setup. 100810 moved SNS to top choice, improved messages.
#120201 rodin.s: internationalized.
#121029 01micko, rerwin: added/integrated Frisbee.
#121122 rerwin: change indicator of frisbee; ensure frisbee stopped if not chosen.
#130103 rerwin: change name of frisbee invocations for integrated version.
#131216 zigbert: gui (gtkdialog) improvements.
#160120 rerwin: add use of new frisbee interface; remove irrelevant dialup code.
#170308 rerwin: use only new frisbee (1.4+) & pgprs (2.0+) interfaces; remove gkdial check; remove unused 'CURRCHOICE code.
#170309 rerwin: retain choice in case multiple setups tried; disconnect any unchosen network setup.
#170418 rerwin: leave defaultconnect unchanged when trying another tool.
#170510 rerwin: exit if executable/dialog killed.

export TEXTDOMAIN=connectwizard_2nd
export OUTPUT_CHARSET=UTF-8

function cn_2nd() { #EXIT ICON.SVG TITLE MSG
	#BORDER='border-width="7"'
	echo '
    <hbox '${BORDER}' space-expand="true" space-fill="true">
      <text space-expand="false" space-fill="false"><label>""</label></text>
      <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'"${3}"'"</label></text>
      <vbox space-expand="false" space-fill="false">
        <text space-expand="false" space-fill="false"><label>""</label></text>
        <button>
          '"$(/usr/lib/gtkdialog/xml_button-icon "${2}" huge)"'
          <action type="exit">'${1}'</action>
        </button>
      </vbox>
    </hbox>
    <hseparator></hseparator>'
}

if [ -d /usr/local/simple_network_setup ];then
	SNS="$(cn_2nd FLAGSNS network.svg "$(gettext '<b><span color='"'blue'"'>Simple Network Setup</span></b>.
This is a simple network setup tool created by BarryK. Not as many features as the Network Wizard, for example requires the network to have a DHCP server (the vast majority of cases). The greatest feature is extreme ease of use. <b>Recommended for most people.</b>')")"
fi

if [ -x /usr/local/bin/frisbee ];then #Jemimah's wireless connection gui. 121122 130103 160120
	FRISBEE="$(cn_2nd FLAGFRISBEE internet_connect_yes.svg "$(gettext '<b><span color='"'blue'"'>Frisbee</span></b>.
This is a tool developed by Jemimah. Mostly for wireless connectivity for laptops, but also can handle ethernet connection. <b>Very good for wireless roaming.</b>')")"
fi

if [ -f /usr/sbin/net-setup.sh ];then #wizard developed by Dougal.
	NETWIZARD="$(cn_2nd FLAGNETWIZARD network_connect.svg "$(gettext '<b><span color='"'blue'"'>Network Wizard</span></b>.
This is the most sophisticated tool for network setup in Puppy. It is for both wired (ethernet) and wireless connectivity, <b>with more configuration features</b> than Frisbee and SNS.')")"
fi

export Network_Connection_Wizard='
<window title="'$(gettext 'Network Connection Wizard')'" icon-name="gtk-connect">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed network_connect.svg 48 "$(gettext 'This build of Puppy has the following choices of GUI tool that you can use to connect to a network (and Internet)...')"`"' 

  <vbox space-expand="true" space-fill="true">
   '${SNS}'
   '${FRISBEE}'
   '${NETWIZARD}'
  </vbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <action>exit:Cancel</action>
    </button>
  </hbox>
</vbox>
</window>'

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
RETSTRING="`gtkdialog -p Network_Connection_Wizard`" 
[ $? -gt 2 ] && exit #170510 dialog killed

if [ "`echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG'`" != "" ];then
 CHOSENWIZ="$(echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG' | cut -f 2 -d \")"
 DEFAULTEXEC="`cat /usr/local/bin/defaultconnect | tail -n 1 | tr -s " " | cut -f 2 -d " "`" #170309...
 if [ -f /root/.connectwizardrc ];then
  . /root/.connectwizardrc #sets CURRENT_EXEC
 else
  CURRENT_EXEC="$DEFAULTEXEC"
 fi #170309 end
 case $CHOSENWIZ in
  FLAGFRISBEE) #121029
   NWEXEC='frisbee' #130103
   NWCHOICE="Jemimah's Frisbee"
   NWSETUP="/usr/local/bin/frisbee" #170309
  ;;
  FLAGNETWIZARD) #net-setup.sh
   NWEXEC='net-setup.sh'
   NWCHOICE="Dougal's Network Wizard"
   NWSETUP="/usr/sbin/net-setup.sh" #170309
  ;;
  FLAGSNS)
   NWEXEC='sns'
   NWCHOICE="Barry's Simple Network Setup"
   NWSETUP="/usr/sbin/sns" #170309
  ;;
 esac

 if [ "$NWEXEC" = 'sns' ];then
  $NWSETUP
 else
  $NWSETUP > /dev/null 2>&1
 fi #170309 end
 [ $? -gt 2 ] && exit #170510 exec killed

 IFSUP="`ifconfig | grep -E '^eth|^wlan'`"
 if [ "$IFSUP" != "" ];then
  IFSUP="`echo "$IFSUP" | cut -f 1 -d ' ' | tr '\n' ' '`"
  MSG1="$(gettext 'These interfaces are active:')
<b>${IFSUP}</b>

`gettext \"...Satisfied? Got online OK? If yes, click <u>Set as default</u> button, so Puppy will be optimised to use this tool for future boots.\"`"
 else
  MSG1="`gettext \"No interfaces are active, so was the tool unsatisfactory? If it actually was OK, click the <u>Set as default</u> button. But, it seems not, so...\"`"
 fi
 
  export Network_Connection_Wizard='
<window title="'$(gettext 'Network Connection Wizard')'" icon-name="gtk-connect">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed network_connect.svg 60 "$(gettext 'You have finished running')" "<b>${NWCHOICE}</b>"`"' 
  <vbox space-expand="true" space-fill="true">
    <frame>
      '"`/usr/lib/gtkdialog/xml_pixmap dialog-question.svg popup`"' 
    <text use-markup="true"><label>"'${MSG1}'

<u>'$(gettext "Try a different tool")'</u> '$(gettext "if you would like to try one of the other network setup tools...")'"</label></text>
   </frame>
   <hbox space-expand="false" space-fill="false">
     <button space-expand="false" space-fill="false">
       '"`/usr/lib/gtkdialog/xml_button-icon refresh`"'
       <label>'$(gettext 'Try a different tool')'</label>
       <action type="exit">FLAGTRYDIFF</action>
     </button>
     <text space-expand="true" space-fill="true"><label>""</label></text>
     <button space-expand="false" space-fill="false">
       <label>'$(gettext "Cancel")'</label>
       '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
       <action>exit:Cancel</action>
     </button>
     <button>
       '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
       <label>'$(gettext 'Set as default')'</label>
       <action type="exit">FLAGYES</action>
     </button>
  </hbox>
</vbox>
</vbox>
</window>'

  RETSTRING="`gtkdialog -p Network_Connection_Wizard --center`" 
  [ $? -gt 2 ] && exit #170510 dialog killed

  if [ "`echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG'`" != "" ];then
   CHOSENACTION="`echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG' | cut -f 2 -d '"'`" #'geany
   case $CHOSENACTION in
    FLAGYES)
     echo -e "#!/bin/sh\nexec ${NWEXEC}" > /usr/local/bin/defaultconnect
     echo "<b>${NWCHOICE} $(gettext "now set as default network tool.</b>

Here are some technical notes that you might find useful:

The default network tool is specified in file /usr/local/bin/defaultconnect, and if you were to look in that file now you will see that it contains '${NWEXEC}'. If you ever wanted to manually change that back to the original setting, you could edit the file and in place of '${NWEXEC}' put 'connectwizard'.

If you click on the 'connect' icon on the desktop, it will directly start the '${NWEXEC}' tool. In fact, it starts whatever has been specified in /usr/local/bin/defaultconnect. You can right-click on the 'connect' icon for a menu with other run options.

<b>Geek stuff</b>: If you really want to know in-depth about the inner workings, specifically how the default tool is activated at bootup, look in /etc/rc.d/rc.sysinit. Two other relevant scripts are /usr/sbin/connectwizard and /usr/sbin/connectwizard_2nd.")" > /tmp/box_help

     /usr/lib/gtkdialog/box_help "$(gettext 'Network Connection Wizard')" info.svg &
    ;;
    FLAGTRYDIFF)
     exec connectwizard
    ;;
   esac
  fi
 
fi

###END###
